# Shows a 'straight EM' style server, where each request alternately does a large or
# small request to the DB.
# it also uses asymy, meaning that it should be fast.
# this one doesn't use a pool but fires up a new connection to the server with each request
# so not totally efficient :)
require 'rubygems'
require 'eventmachine'
require 'source/asymy/asymy'
require 'pp'
require 'rubygems'
require 'connection_pool'


module DoOneSql
  @@count_ever = 0

  def post_init
     @@count_ever += 1
     @my_count = @@count_ever
     print 'got incoming', @my_count
     @conn = Asymy::Connection.new $opts
     if rand(2) == 1
	go :big
     else
	go :small
     end
     
  end

  def go size
	print "start #{size}"
	case size
	when :big
		db = 'user_sessions'
	else
		db = 'users'
	end
	query  =  "select count(*) from #{db}"
	@conn.exec(query) { |i, c|
		print "done #{size} #{@my_count}\n"
		@conn.em_connection.close_connection
		self.close_connection
  	}

  end

end



EventMachine::run {
   $opts = {:target => "localhost",
                             :port => 3306,
                             :username => "wilkboar_ties",
                             :password => "ties",
                             :database => "local_leadgen_dev"}
  
  EM.start_server "0.0.0.0", 10000, DoOneSql
   
} # EM run
